package com.android.kotlin.activity


import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import com.android.kotlin.R
import com.android.kotlin.base.LifecycleActivity
import com.android.kotlin.fragment.FragmentFactory
import com.android.kotlin.viewmodel.MainViewModel
import com.android.kotlin.util.user.LoginSucListener
import com.android.kotlin.util.user.LoginSucState
import kotlinx.android.synthetic.main.activity_main.*
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener

class MainActivity : LifecycleActivity<MainViewModel>(), LoginSucListener {

    private var mFragmentManager:FragmentManager? =null
    private var mFragmentFactory: FragmentFactory? = null


    override fun getLayoutId(): Int= R.layout.activity_main

    override fun initView() {
        super.initView()
        LoginSucState.addListener(this)//注册对登录的监听
        mFragmentManager=supportFragmentManager
        mFragmentFactory = FragmentFactory(mFragmentManager!!)
        switchContent(0)
        initBottomTab()
    }

    fun initBottomTab(){
       var tab= pager_bottom_tab.material()
            .addItem(R.drawable.ic_home,"首页",resources.getColor(R.color.colorPrimary))
            .addItem(R.drawable.ic_wechat,"公众号",resources.getColor(R.color.colorPrimary))
            .addItem(R.drawable.ic_system,"体系",resources.getColor(R.color.colorPrimary))
            .addItem(R.drawable.ic_navigation,"导航",resources.getColor(R.color.colorPrimary))
            .addItem(R.drawable.ic_project,"项目",resources.getColor(R.color.colorPrimary))
            .setDefaultColor(resources.getColor(R.color.color_1c1c1c))
            .build()
            tab.addTabItemSelectedListener(object: OnTabItemSelectedListener {
                override fun onSelected(index: Int, old: Int) {
                    switchContent(index)
                }

                override fun onRepeat(index: Int) {
                }
            })
    }


    /**
     * fragment切换
     */
    fun switchContent(resId: Int): Fragment {
        val fragment = mFragmentFactory?.instantiateItem(
            frameLayout, resId
        ) as Fragment
        mFragmentFactory?.setPrimaryItem(frameLayout, 0, fragment)
        mFragmentFactory?.finishUpdate(frameLayout)
        return fragment
    }

    override fun initData() {
        super.initData()
        dataObserver()
    }


    override fun dataObserver() {

    }




    override fun getLoadSirView(): Any {
        return this
    }

    /**
     * 用户登录成功
     * */
    override fun success(name: String, collectIds: List<Int>?) {
//        username.text=name
    }


    override fun onDestroy() {
        super.onDestroy()
        LoginSucState.removeListener(this)//取消对登录的监听
    }



}
